package de.tum.team26.eistpoll.client.UI;

import java.util.LinkedList;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import de.tum.team26.eistpoll.client.EIST_POLL;
import de.tum.team26.eistpoll.shared.PollData;

/**
 * Class to display the Admin Control Panel
 */
public class AdminCP extends UIElement {

	//Controller
	EIST_POLL controller;

	// Container
	VerticalPanel adminCPPanel = new VerticalPanel();
	VerticalPanel topPanel = new VerticalPanel();
	VerticalPanel topPanelFields = new VerticalPanel();
	
	// UI Elements
	Label headding = new Label("Add a Poll");
	Label lblPoll = new Label("Title:");
	TextBox tbxTitle = new TextBox();
	HorizontalPanel headder = new HorizontalPanel();
	Label btnLogin = new Label("Login");
	Image tum = new Image();
	Image info = new Image();
	Button addPoll = new Button("Add");
	Label lblErrMsg;
	
	// Element to handle Results
	Results results;
	
	/**
	 * Constructor to initiate UI
	 * @param controller Controller for the MVC Pattern
	 */
	public AdminCP(EIST_POLL controller) {
		// Initialise UI
		this.controller = controller;
		adminCPPanel = new VerticalPanel();
		
		tum.setStyleName("tumlogo");
		info.setStyleName("infologo");
		tum.setUrl("tum.gif");
		info.setUrl("info.gif");
		
		headder.setStyleName("headder");
		headder.add(tum);
		headder.add(info);
		topPanel.add(headder);
		headding.setStyleName("h1");
		topPanel.add(headding);
		topPanelFields.add(lblPoll);
		topPanelFields.add(tbxTitle);
		addPoll.addStyleDependentName("submit");
		topPanelFields.setStyleName("loginInput");
		topPanel.add(topPanelFields);
		topPanel.add(addPoll);
		topPanel.addStyleName("containeros");

		addPoll.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				addPoll();
			}
		});
		
		adminCPPanel.add(topPanel);
		results = new Results(true, controller);
		adminCPPanel.add(results.getContainer());
	}
	
	public void addPoll(){
		controller.addPoll(tbxTitle.getText());
	}

	public void updateResults(LinkedList<PollData> result) {
		results.updateResults(result);
	}

	@Override
	public VerticalPanel getContainer() {
		return adminCPPanel;
	}

	@Override
	public void displayErrorMessage(String message) {
		if (lblErrMsg == null){
			lblErrMsg = new Label(message);
			lblErrMsg.setStyleName("error");
			adminCPPanel.add(lblErrMsg);
		}
		else{
			lblErrMsg.setText(message);
		}
	}
}
